package cn.ylz.ylmall.content.service.impl;

import cn.ylz.ylmall.account.pojo.vo.PageData;
import cn.ylz.ylmall.account.security.CurrentPrincipal;
import cn.ylz.ylmall.common.ex.ServiceException;
import cn.ylz.ylmall.common.web.ServiceCode;
import cn.ylz.ylmall.content.dao.persist.repository.IContentRepository;
import cn.ylz.ylmall.content.pojo.entity.Content;
import cn.ylz.ylmall.content.pojo.param.ContentAddNewParam;
import cn.ylz.ylmall.content.pojo.vo.ContentListItemVO;
import cn.ylz.ylmall.content.pojo.vo.ContentStandardVO;
import cn.ylz.ylmall.content.pojo.vo.CountVO;
import cn.ylz.ylmall.content.service.IContentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
@Slf4j
public class ContentServiceImpl implements IContentService {

    @Value("5")
    private Integer defaultQueryPageSize;
    @Autowired
    private IContentRepository contentRepository;

    /**
     * 新增内容
     * @param currentPrincipal
     * @param remoteAddr
     * @param contentAddNewParam
     */
    @Override
    public void addNew(CurrentPrincipal currentPrincipal, String remoteAddr, ContentAddNewParam contentAddNewParam) {
        Content content = new Content();
        BeanUtils.copyProperties(contentAddNewParam, content);
        content.setCreateBy(currentPrincipal.getId());
        content.setViewCount(0);
        content.setLikeCount(0);
        content.setCollectCount(0);
        content.setCommentCount(0);
        content.setBrief("");
        content.setType(1);
        content.setCreateTime(new Date());
        System.out.println("检测插入数据是否正确"+content);
        int rows = contentRepository.insert(content);
        if(rows !=1){
            String message = "发布失败,请重新发布";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除文章】的请求，参数：{}", id);
        ContentStandardVO queryResult = contentRepository.getStandardById(id);
        if(queryResult == null){
            String message = "删除失败,尝试删除的内容不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
        int rows = contentRepository.deleteById(id);
        if(rows !=1){
            String message = "删除失败";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public ContentStandardVO getStandardById(Long id) {
       log.debug("开始处理【根据ID查询文章】的请求，参数：{}", id);
       ContentStandardVO queryResult = contentRepository.getStandardById(id);
       if(queryResult == null){
           String message = "查询失败,尝试查询的内容不存在";
           log.warn(message);
           throw new ServiceException(ServiceCode.ERROR_DELETE, message);
       }

       int rows = contentRepository.updateViewCount(id);
       System.out.println("rows = " + rows);
       if(rows != 1){
           String message = "更新浏览量失败";
           log.warn(message);
           throw new ServiceException(ServiceCode.ERROR_INSERT, message);
       }
       return queryResult;
    }

    /**
     * 查询内容列表
     * @param pageNum
     * @return
     */
    @Override
    public PageData<ContentListItemVO> list(Integer pageNum) {
        log.debug("开始处理【获取文章列表】的请求,页码:{}", pageNum);
        PageData<ContentListItemVO> pageData = contentRepository.list(pageNum,defaultQueryPageSize);
        return pageData;
    }

    /**
     * 按分类查询
     * @param categoryId
     * @param pageNum
     * @return
     */
    @Override
    public PageData<ContentListItemVO> listByCategoryId(Long categoryId, Integer pageNum) {
        log.debug("开始处理【根据文章类别查询文章列表】的业务，文章类别：{}, 页码：{}", categoryId, pageNum);
        PageData<ContentListItemVO> pageData = contentRepository.listByCategoryId(categoryId, pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public void update(Long id, Long likeCount, Long commentCount, Long collectCount) {
        contentRepository.update(id,likeCount,commentCount,collectCount);
    }

    @Override
    public void updateContent(Long id,ContentAddNewParam contentAddNewParam) {
        log.debug("Serivice，参数：{}", contentAddNewParam);
        //先查询是否存在
        ContentStandardVO queryResult = contentRepository.getStandardById(id);
        log.debug("queryResult：{}", queryResult);
        if(queryResult == null){
            String message = "你更新的内容不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
        int rows = contentRepository.updateContent(id,contentAddNewParam);
        if(rows !=1){
            String message = "更新失败";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}
